package sdk.android.callrecorder;

import java.io.FileInputStream;
import java.io.IOException;

import sdk.android.callrecorder.data.RecordHistory;
import sdk.android.callrecorder.utils.LogUtils;
import android.app.Dialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class RecordVoicePlayerDialog extends Dialog {

	private RecordHistory rh;

	private FileInputStream fis;
	
	private Button buttonPlayStop;
    private MediaPlayer mediaPlayer;
    private SeekBar seekBar;
    
    private final Handler handler = new Handler();
 
    public RecordVoicePlayerDialog(Context context, RecordHistory rh) {
            super(context);
            setContentView(R.layout.dialog_player);
            
            this.rh = rh;
            
            initViews();  
    }
    
    @Override
	public void dismiss() {
		super.dismiss();
		
		if(fis!=null){
			try {
				fis.close();
			} catch (IOException e) {
				LogUtils.error(e);
			}
		}
	}
 
    // This method set the setOnClickListener and method for it (buttonClick())
    private void initViews() {
        buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
        buttonPlayStop.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				buttonClick();
			}
		});
        
        mediaPlayer = new MediaPlayer();
        mediaPlayer.reset();
        
        try {
        	fis = new FileInputStream(rh.file_path);
        	
            mediaPlayer.setDataSource(fis.getFD());
			mediaPlayer.prepare();
		} catch (Exception e) {
			LogUtils.error(e);
		} 
        
        TextView tvCurrent = (TextView) findViewById(R.id.tv_current_time);
        tvCurrent.setText("00:00");
        
        TextView tvMax = (TextView) findViewById(R.id.tv_max_time);
        tvMax.setText(String.valueOf(mediaPlayer.getDuration()/1000));
        
        seekBar = (SeekBar) findViewById(R.id.SeekBar01);
        seekBar.setMax(mediaPlayer.getDuration());
        seekBar.setOnTouchListener(new OnTouchListener() {@Override public boolean onTouch(View v, MotionEvent event) {
        	seekChange(v);
			return false; }
		});
 
    }
    
    public void startPlayProgressUpdater() {
    	seekBar.setProgress(mediaPlayer.getCurrentPosition());
    	
		if (mediaPlayer.isPlaying()) {
			Runnable notification = new Runnable() {
		        public void run() {
		        	startPlayProgressUpdater();
				}
		    };
		    handler.postDelayed(notification,1000);
    	}else{
    		mediaPlayer.pause();
    		buttonPlayStop.setText(getContext().getString(R.string.play_str));
    		seekBar.setProgress(0);
    	}
    } 
 
    // This is event handler thumb moving event
    private void seekChange(View v){
    	if(mediaPlayer.isPlaying()){
	    	SeekBar sb = (SeekBar)v;
			mediaPlayer.seekTo(sb.getProgress());
		}
    }
 
    // This is event handler for buttonClick event
    private void buttonClick(){
    	
        if (buttonPlayStop.getText() == getContext().getString(R.string.play_str)) {
            buttonPlayStop.setText(getContext().getString(R.string.pause_str));
            try{
            	mediaPlayer.start();
                startPlayProgressUpdater(); 
            }catch (IllegalStateException e) {
            	mediaPlayer.pause();
            }
        }else {
            buttonPlayStop.setText(getContext().getString(R.string.play_str));
            mediaPlayer.pause();
        }
    }
}
